Ext.define('appmod.view.framework.MainMenuPanel', {
    extend: 'Ext.panel.Panel',
    alias: 'widget.mainmenupanel',

    initComponent: function() {
        var me = this;
		
		
        if (Ext.isString(me.store)) {
            me.store = Ext.StoreMgr.lookup(me.store);
        } else if (!me.store || Ext.isObject(me.store) && !me.store.isStore) {
            me.store = new Ext.data.TreeStore(Ext.apply({}, me.store || {}, {
                root: me.root,
                fields: me.fields,
                model: me.model,
                folderSort: me.folderSort
            }));
        };
		
		// --- create store ---
		var tpl = me.buildMainmenuTemplate();
		
		var authData = me.filterAuthPermissionMenuData(MENURESOURCES , MENURESOURCES_AUTH);

        Ext.applyIf(me, {
			// use timplate with data
			tpl:tpl,
			data : authData,
			menuRes : authData,
			authMenu : MENURESOURCES_AUTH,
			dockedItems:[
			{
				xtype:'toolbar',
				dock:'bottom',
				ui:'footer',
				style:'height:26px'
			}
			],
			listeners:{
				afterrender : {
					fn:me.addEventForMainmenu
				}
			}						
        });
		
        me.callParent(arguments);
		
    },
	
	/**
	 * res set the menu resource ---
	 * @param {Object} menuRes
	 * @param {Object} menuResAuth
	 */
	filterAuthPermissionMenuData:function(menuRes , menuResAuth) {
		var filterData = [];
		for (var i = 0 ; i < menuRes.length ; i++) {
			var menu = menuRes[i];
			
			var perm = this.checkResPermission(menu , 'access' , menuResAuth);

			if ( perm ) {
				
				filterData.push( menu );
			}
		}
		
		filterData.sort(function(aMenu , bMenu) {
			if (aMenu.sortNum > bMenu.sortNum) {
				return 1;
			} else if (aMenu.sortNum < bMenu.sortNum){
				return -1;
			}
			return 0;
		});

		return filterData;
	},
	


	checkResPermission:function(menu , definedAcl , menuResAuth) {
		// --- get auth data ---
		var authData = menuResAuth;
		
		var perm = null;
		for (var i = 0 ; i < authData.length ;i++) {
			perm = authData[i];
			if (menu.id == perm.id) {
				if ( definedAcl == 'deny') {
					return false;
				}
				else if ( definedAcl == 'access' ) {
					if (perm.acl == 'access') {
						return true;
					}
					else if (perm.acl == 'deny') {
						return false;
					}
					else {
						return false;
					}
				} 
			}
		}
		// --- default return true ---
		return true;
						
	},
	
	getAuthMenuData:function() {
		return this.authMenu;
	},
	
	
	addEventForMainmenu:function() {
		me = this;
		
		var elems = Ext.select("td[class*=menuTab]");
		for (var i = 0 ; i < elems.getCount() ; i++) {
			var menuElem = elems.item(i);
			menuElem.applyStyles("cursor:hand");
			menuElem.on('click',me.eventForClickMainMenu,me,{currentId: menuElem.dom.id});
		}
	},
	
	/**
	 * private event -
	 */
	eventForClickMainMenu:function( e, htmlelem, eOpts) {
		me = this;
		
		var elems = Ext.select("td[class*=menuTab]");
		for (var j = 0 ; j < elems.getCount() ; j++ ) {
			var elem = elems.item(j);
			elem.removeCls('selected');
			if (elem.dom.id == eOpts.currentId) {
				elem.addCls('selected');
				// --- add the event ---
				data = me.menuRes;

				if (data) {
					for (var i = 0 ; i < data.length ; i++) {
						var menu = data[i];
						if (menu.id == eOpts.currentId) {
							// --- no children
							if (menu.leaf) {
								// --- find the panel ---
								me.fireEvent('execAction',{
									menu:menu
								});
							}
							else {
								me.fireEvent('opensubmenu',{
									menu:menu
								});								
							}
						
							break;
						}
					}
				}
				
				
			}
		}
		
		

	},
	
	
	
	buildMainmenuTemplate:function() {
		
		
		var tpl = Ext.create('Ext.XTemplate',
			[
			'<div id="topmenutabs">',
			'<table id="topmenutabstable" border="0" width="100%" cellspacing="0" cellpadding="0">',
			'<tr>',
			' <td class="menuBackground">&nbsp;</td>',
			'<tpl for=".">',
			'<td class="menuTab" id="{id}" />',
			'<div class="menuTabInnerContainer" >',
			'<div class="corner left"></div>',
			'<div class="corner right"></div>',
			'<div class="bottomLeftCorner"></div>',	
			'<div class="bottomRightCorner"></div>',
				'<a href="#" >{text}</a>',
			'</div>',
			'</td>',
			'</tpl>',
			'<td width="100%" class="menuBackground"><img border="0" src="../../images/blank.gif" /></td>',
			'</tr>',
			'</table>',
			'</div>'
			]
		);
		return tpl;
	}


	
	
	
	
	
});